//
//  JGValidateLoginContentView.m
//  JGRider
//
//  Created by 单启志 on 2018/3/2.
//  Copyright © 2018年 ffq. All rights reserved.
//

#import "JGValidateLoginContentView.h"
#import "FQCountDownButton.h"
#import "FQGetValidateCodeHelper.h"
@interface JGValidateLoginContentView()
@property (weak, nonatomic) IBOutlet UITextField *accountTF;//账号
@property (weak, nonatomic) IBOutlet UITextField *validateCodeTF;//验证码
@property (weak, nonatomic) IBOutlet UIButton *validateCodeBtn;//验证码btn
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@property(nonatomic,copy)NSString *realValidateCode;
@end
@implementation JGValidateLoginContentView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        self=[[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil]lastObject];
        self.frame=frame;
        [self layoutSubViews];
    }
    return self;
}

-(void)awakeFromNib{
    [super awakeFromNib];
}

-(void)layoutSubViews{
    self.loginBtn.layer.cornerRadius=AdaptedWidth(3);
    self.loginBtn.layer.masksToBounds=YES;
    self.validateCodeBtn.layer.cornerRadius=AdaptedWidth(5);
    self.validateCodeBtn.layer.masksToBounds=YES;
    self.validateCodeBtn.layer.borderColor=HEXCOLOR(0xfea129).CGColor;
    self.validateCodeBtn.layer.borderWidth=AdaptedHeight(0.5);
    [self.validateCodeBtn setTitleColor:HEXCOLOR(0xfea129) forState:UIControlStateNormal];
    [self.validateCodeBtn setTitleColor:HEXCOLOR(0xffffff) forState:UIControlStateDisabled];
}
//获取验证码
- (IBAction)onValidateCodeBtnClick:(FQCountDownButton*)sender {
    FQWeakSelf(self);
    [[FQGetValidateCodeHelper share]getValidateCodeWithMobile:[self.accountTF.text stringByTrim] view:self button:sender sucess:^(NSString *code) {
        weakself.realValidateCode=code;
    } faile:nil];

}

//登录
- (IBAction)onLoginBtnClick:(id)sender {
    
    if ([self validateInput]==NO) {
        return;
    }
    if ([self.delegate respondsToSelector:@selector(contentViewLoginEventWithParams:)]) {
        NSMutableDictionary *dic=[NSMutableDictionary dictionary];
        dic[@"roleId"]=roleId;
        dic[@"account"]=[self.accountTF.text stringByTrim];
        dic[@"code"]=[self.validateCodeTF.text stringByTrim];
        [self.delegate contentViewLoginEventWithParams:dic];
    }
}
//注册
- (IBAction)onRegistBtnClick:(id)sender {
    if ([self.delegate respondsToSelector:@selector(contentViewRegistEvent)]) {
        [self.delegate contentViewRegistEvent];
    }
}


#pragma mark -  验证输入框合法性
-(BOOL)validateInput{
    if([self validateWithFT:self.accountTF withMessage:@"请输入登录账号"]==NO){
        return NO;
    }
    if([self validateWithFT:self.validateCodeTF withMessage:@"请输入验证码"]==NO){
        return NO;
    }
    if ([self validateCode]==NO) {
        return NO;
    }
    return YES;
}
-(BOOL)validateCode{
    if (FQIsEmpty(self.realValidateCode)) {
        [MBProgressHUD showError:@"请先获取验证码" ToView:self];
        return NO;
    }
    if ([self.validateCodeTF.text isEqualToString:self.realValidateCode]) {
        return YES;
    }else{
        [MBProgressHUD showError:@"验证码错误" ToView:self];
        return NO;
    }
}
//验证输入框非空
-(BOOL)validateWithFT:(UITextField *)tf withMessage:(NSString*)message{
    if (![tf.text.stringByTrim isEqualToString:@""]&&[tf.text isNotBlank]) {
        return YES;
    }else{
        [MBProgressHUD showError:message ToView:self];
        return NO;
    }
}

@end
